use markdown_elements::Markdown;
use markdown_parser::parse;
use once_cell::sync::Lazy;
static TABLE_S: Lazy<std::string::String> = Lazy::new(|| {
    r#"
<table>
    <tr>
        <th>a</th>
        <th>b</th>
        <th>c</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>
    <tr>
        <td>5</td>
        <td></td>
        <td>7</td>
        <td>8</td>
    </tr>
    <tr>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>
</table>
"#
    .replace([' ', '\t', '\n'], "")
});
#[test]
fn test_markdown_table() {
    let input = r#" 
                |a|b|c|
                |--|:--|--:|:--:|
                |1|2|3|4|
                |5|6|7|8|
                |5||7|8|
                |5|6|7|8|
        "#
    .trim();
    let input = format!("{}\n", input);
    // println!("[source beign]\n{}[source end]",input);
    let (res, errs) = parse(&input);
    assert!(res.is_some());
    if let Some(res) = res {
        assert_eq!(res.len(), 1);
        assert!(
            matches!(
                res[0],
                Markdown::Table {
                    aligns: _,
                    heads: _,
                    rows: _
                }
            ),
            "{:?}",
            res
        );
        let left = res
            .iter()
            .fold("".to_string(), |acc, e| format!("{}{}", acc, e));
        assert_eq!(left, TABLE_S.to_string())
    } else {
        panic!("未成功制造出表格");
    }
    if let Some(errs) = errs {
        panic!("{}", errs)
    }
}

#[test]
fn test_markdown_table_link() {
    let input = r#" 
                |[a](b)|b|
                |--|:--|
                |1|2|
        "#
    .trim();
    let input = format!("{}\n", input);
    // println!("[source beign]\n{}[source end]",input);
    let (res, errs) = parse(&input);
    assert!(res.is_some());
    if let Some(res) = res {
        assert_eq!(res.len(), 1);
        assert!(
            matches!(
                res[0],
                Markdown::Table {
                    aligns: _,
                    heads: _,
                    rows: _
                }
            ),
            "{:?}",
            res
        );
        let left = res
            .iter()
            .fold("".to_string(), |acc, e| format!("{}{}", acc, e));
        assert_eq!(
            left,
            r#"<table><tr><th><a href="b">a</a></th><th>b</th></tr><tr><td>1</td><td>2</td></tr></table>"#
        )
    } else {
        panic!("未成功制造出表格");
    }
    if let Some(errs) = errs {
        panic!("{}", errs)
    }
}
